﻿using UnityEngine;
using System.Collections;
using Game.Scripts.Managers;
using System;

namespace FrenzyIsland.UI
{
    public class UIMessageBoxBuildingLevelUp : UIMessageBox
    {
        public Building.EnumLevelUpError status = Building.EnumLevelUpError.Undefined;

        private bool requireRefresh = false;
        private Resource[] resources = null;

        public Building building;
        public UITable tablePrima;
        public UITable tableDopo;
        public UIGrid gridCosto;
        public UIAtlas iconAtlas;
        public UIFont fontResource;
        
        public override void Start()
        {
            requireRefresh = true;
            base.Start();
        }

        public void Update()
        {
            if( requireRefresh == true && building != null)
            {
                //esiste un livello successivo
                //inserisco il costo dell'upgrade
                resources = building.prototype.GetListCost((int)building.level + 1);

                if ( resources != null )
                {
                    int height = gridCosto.transform.parent.gameObject.GetComponent<UIWidget>().height;
                    foreach (Resource res in resources)
                    {
                        base.CreateSprite(gridCosto.gameObject, iconAtlas, ResourceManager.instance.mappingIconResource[res.type], height);
                        base.CreateLabel(gridCosto.gameObject, fontResource, res.amount.ToString(), height, null);
                    }

                    gridCosto.Reposition();

                    //produzione del livello successivo
                    AddProductionLevel(tableDopo, NextLevel);

                    //chiedo se posso fare il level up
                    status = building.CanLevelUP();
                }
                else
                {
                    //non ci sono livelli successivi
                    status = Building.EnumLevelUpError.NoLevelAvaible;
                    base.CreateLabel(tableDopo.gameObject, fontResource, "N.d.", 50, 150);
                    tableDopo.repositionNow = true;
                    tableDopo.Reposition();
                }
               
                //inserisco le produzioni del livello attuale (sempre disponibile)
                AddProductionLevel(tablePrima, Level);                    
                requireRefresh = false;
            }
        }
        
        public void AddProductionLevel(UITable table, int livello)
        {
            float production = (float)Math.Round ( building.GetEstimateResourceProductionPerMinute(livello) , 2 );
            Resource res = building.prototype.resourceProduced;

            base.CreateSprite(table.gameObject, iconAtlas, ResourceManager.instance.mappingIconResource[building.prototype.resourceProduced.type], 50);
            base.CreateLabel (table.gameObject, fontResource, production.ToString() + "/m", 50, 150 );

            table.repositionNow = true;
            table.Reposition();
        }

        #region Binding

        public string Name {
            get
            {
                if (building == null) return "-";
                return building.prototype.name;
            }
        }

        public string IconName {
            get
            {
                if (building == null) return "Question-mark-icon";
                return building.prototype.iconName;
            }
        }

        public int Level
        {
            get
            {
                if (building == null) return 0;
                return (int)building.level;
            }
        }

        public int NextLevel
        {
            get
            {
                if (building == null) return 0;
                return (int)building.level+1;
            }
        }

        public string Messaggio {
            get
            {
                switch (status)
                {
                    case Building.EnumLevelUpError.Undefined:
                        return "Pending";
                    case Building.EnumLevelUpError.NoResource:
                        return "Non hai abbastanza risorse per migliorare l'edificio";
                    case Building.EnumLevelUpError.NoLevelAvaible:
                        return "Hai raggiunto il livello massimo per l'edificio";
                    case Building.EnumLevelUpError.OK:
                        return "OK";
                }
                return "N.d.";
            }
        }

        public float AlfaCosto
        {
            get
            {
                if (status == Building.EnumLevelUpError.OK) return 1;
                return 0;
            }
        }

        public bool OkActive
        {
            get
            {
                if (status == Building.EnumLevelUpError.OK) return true;
                return false;
            }
        }

        public float AlfaMessage
        {
            get
            {
                if (status == Building.EnumLevelUpError.NoLevelAvaible || 
                    status == Building.EnumLevelUpError.NoResource) return 1;
                return 0;
            }
        }

        public UIButton.State StateButton
        {
            get
            {
                if (status == Building.EnumLevelUpError.OK )
                    return UIButtonColor.State.Normal;

                return UIButtonColor.State.Disabled;
            }
        }

        public Color ColorCheck
        {
            get
            {
                if( OkActive )
                    return new Color(1f, 1f, 1f);
                else
                    return new Color(0.5f, 0.5f, 0.5f);
            }
        }

        #endregion Binding

    }
}